package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NAsphaltMatchBo;
import com.ruoyi.zhgd.domain.vo.NAsphaltMatchVo;
import com.ruoyi.zhgd.domain.NAsphaltMatch;
import com.ruoyi.zhgd.mapper.NAsphaltMatchMapper;
import com.ruoyi.zhgd.service.INAsphaltMatchService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青混合料配比表Service业务层处理
 *
 * @author tll
 * @date 2023-10-12
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NAsphaltMatchServiceImpl implements INAsphaltMatchService {

    private final NAsphaltMatchMapper baseMapper;

    /**
     * 查询沥青混合料配比表
     */
    @Override
    public NAsphaltMatchVo queryById(String id){
        if (ObjectUtils.isEmpty(id)) {
            throw new ServiceException("查询id不能为空");
        }
        NAsphaltMatchVo data = baseMapper.getById(id);
        return data;
    }

    /**
     * 查询沥青混合料配比表列表
     */
    @Override
    public TableDataInfo<NAsphaltMatchVo> queryPageList(NAsphaltMatchBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NAsphaltMatch> lqw = buildQueryWrapper(bo);
        Page<NAsphaltMatchVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青混合料配比表列表
     */
    @Override
    public List<NAsphaltMatchVo> queryList(NAsphaltMatchBo bo) {
        LambdaQueryWrapper<NAsphaltMatch> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NAsphaltMatch> buildQueryWrapper(NAsphaltMatchBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NAsphaltMatch> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBaseCode()), NAsphaltMatch::getBaseCode, bo.getBaseCode());
        lqw.eq(StringUtils.isNotBlank(bo.getStationCode()), NAsphaltMatch::getStationCode, bo.getStationCode());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectId()), NAsphaltMatch::getProjectId, bo.getProjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getMatchType()), NAsphaltMatch::getMatchType, bo.getMatchType());
        lqw.like(StringUtils.isNotBlank(bo.getMatchName()), NAsphaltMatch::getMatchName, bo.getMatchName());
        lqw.eq(StringUtils.isNotBlank(bo.getMatchCode()), NAsphaltMatch::getMatchCode, bo.getMatchCode());
        lqw.eq(StringUtils.isNotBlank(bo.getSiteCenter()), NAsphaltMatch::getSiteCenter, bo.getSiteCenter());
        lqw.eq(StringUtils.isNotBlank(bo.getPavingLevel()), NAsphaltMatch::getPavingLevel, bo.getPavingLevel());
        lqw.eq(bo.getTemperature() != null, NAsphaltMatch::getTemperature, bo.getTemperature());
        lqw.eq(bo.getTemperatureMax() != null, NAsphaltMatch::getTemperatureMax, bo.getTemperatureMax());
        lqw.eq(bo.getTemperatureMin() != null, NAsphaltMatch::getTemperatureMin, bo.getTemperatureMin());
        lqw.eq(bo.getAsphaltAggregateRatio() != null, NAsphaltMatch::getAsphaltAggregateRatio, bo.getAsphaltAggregateRatio());
        lqw.eq(bo.getAsphaltAggregateError() != null, NAsphaltMatch::getAsphaltAggregateError, bo.getAsphaltAggregateError());
        lqw.eq(bo.getGravel1() != null, NAsphaltMatch::getGravel1, bo.getGravel1());
        lqw.eq(bo.getGravel2() != null, NAsphaltMatch::getGravel2, bo.getGravel2());
        lqw.eq(bo.getGravel3() != null, NAsphaltMatch::getGravel3, bo.getGravel3());
        lqw.eq(bo.getGravel4() != null, NAsphaltMatch::getGravel4, bo.getGravel4());
        lqw.eq(bo.getGravel5() != null, NAsphaltMatch::getGravel5, bo.getGravel5());
        lqw.eq(bo.getGravel6() != null, NAsphaltMatch::getGravel6, bo.getGravel6());
        lqw.eq(bo.getPowder1() != null, NAsphaltMatch::getPowder1, bo.getPowder1());
        lqw.eq(bo.getPowder2() != null, NAsphaltMatch::getPowder2, bo.getPowder2());
        lqw.eq(bo.getPowder3() != null, NAsphaltMatch::getPowder3, bo.getPowder3());
        lqw.eq(bo.getRecycledMaterial() != null, NAsphaltMatch::getRecycledMaterial, bo.getRecycledMaterial());
        lqw.like(StringUtils.isNotBlank(bo.getGravel1Name()), NAsphaltMatch::getGravel1Name, bo.getGravel1Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel2Name()), NAsphaltMatch::getGravel2Name, bo.getGravel2Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel3Name()), NAsphaltMatch::getGravel3Name, bo.getGravel3Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel4Name()), NAsphaltMatch::getGravel4Name, bo.getGravel4Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel5Name()), NAsphaltMatch::getGravel5Name, bo.getGravel5Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel6Name()), NAsphaltMatch::getGravel6Name, bo.getGravel6Name());
        lqw.like(StringUtils.isNotBlank(bo.getPowder1Name()), NAsphaltMatch::getPowder1Name, bo.getPowder1Name());
        lqw.like(StringUtils.isNotBlank(bo.getPowder2Name()), NAsphaltMatch::getPowder2Name, bo.getPowder2Name());
        lqw.like(StringUtils.isNotBlank(bo.getPowder3Name()), NAsphaltMatch::getPowder3Name, bo.getPowder3Name());
        lqw.eq(StringUtils.isNotBlank(bo.getProductionType()), NAsphaltMatch::getProductionType, bo.getProductionType());
        lqw.eq(bo.getHotConfig() != null, NAsphaltMatch::getHotConfig, bo.getHotConfig());
        lqw.eq(bo.getColdConfig() != null, NAsphaltMatch::getColdConfig, bo.getColdConfig());
        lqw.eq(bo.getCreator() != null, NAsphaltMatch::getCreator, bo.getCreator());
        lqw.eq(bo.getCreateDate() != null, NAsphaltMatch::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getUpdater() != null, NAsphaltMatch::getUpdater, bo.getUpdater());
        lqw.eq(bo.getUpdateDate() != null, NAsphaltMatch::getUpdateDate, bo.getUpdateDate());
        return lqw;
    }

    /**
     * 新增沥青混合料配比表
     */
    @Override
    public Boolean insertByBo(NAsphaltMatchBo bo) {
        NAsphaltMatch add = BeanUtil.toBean(bo, NAsphaltMatch.class);
        validEntityBeforeSave(add);
        add.setCreateDate(new Date());
        add.setCreator(LoginHelper.getUserId());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青混合料配比表
     */
    @Override
    public Boolean updateByBo(NAsphaltMatchBo bo) {
        NAsphaltMatch update = BeanUtil.toBean(bo, NAsphaltMatch.class);
        validEntityBeforeSave(update);
        update.setUpdateDate(new Date());
        update.setUpdater(LoginHelper.getUserId());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NAsphaltMatch entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青混合料配比表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据拌合楼、编号获取级配信息
     *
     * @param matchCode 配方编号
     * @return 级配信息
     */
    @Override
    public NAsphaltMatch getByCode(String matchCode) {
        if (ObjectUtils.isEmpty(matchCode)) {
            return null;
        }
        QueryWrapper<NAsphaltMatch> queryWrapper = new QueryWrapper<NAsphaltMatch>();
        queryWrapper.eq("match_code", matchCode);
        List<NAsphaltMatch> list = baseMapper.selectList(queryWrapper);
        if (null == list || 1 != list.size()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public TableDataInfo<NAsphaltMatchVo> page(NAsphaltMatchBo bo, PageQuery pageQuery) {
        IPage<NAsphaltMatchVo> data = baseMapper.page(bo, pageQuery.build());
        return TableDataInfo.build(data);
    }
}
